package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.bean.BeanUtils;
import eu.bitwalker.useragentutils.browser.SafariUtils;
import org.apache.commons.collections4.BagUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    TbClueMapper tbClueMapper;

    @Override
    public void add(ClueTrackRecordVo tbClueTrackRecord) {
        //获取当前用户
        String username = SecurityUtils.getUsername();
        //获取线索id
        Long clueId = tbClueTrackRecord.getClueId();
        if (clueId ==null){
            throw new CustomException("数据异常：线索id缺失");
        }
        //跟新线索
        TbClue clue=new TbClue();
        //拷贝属性
        BeanUtils.copyProperties(tbClueTrackRecord,clue);
        //补全数据
        clue.setUpdateBy(username);
        clue.setUpdateTime(new Date());
        clue.setId(clueId);
        clue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbClueMapper.updateTbClue(clue);
        //添加线索跟进记录
        TbClueTrackRecord clueTrackRecord=new TbClueTrackRecord();
        //拷贝属性
        BeanUtils.copyProperties(tbClueTrackRecord,clueTrackRecord);
        //补全数据
        clueTrackRecord.setCreateBy(username);
        clueTrackRecord.setCreateTime(new Date());
        tbClueTrackRecordMapper.add(clueTrackRecord);
    }

    /**
     * 线索跟进记录查询
     * @param clueId
     * @return
     */
    @Override
    public List<TbClueTrackRecord> list(Long clueId) {
        return tbClueTrackRecordMapper.list(clueId);
    }
}
